package com.sunny.core.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.sunny.core.bean.PublicDict;
import com.sunny.core.service.IPublicDictService;
import com.sunny.core.utils.SpringContextUtil;

public class HtmldicTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	
	private String type;
	private String id;
	private String name;
	private String style;
	private String defaultValue;
	private String pleSelect;
	private String onChange;
	
	IPublicDictService publicDictService;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		
		if (id == null) {
			this.id = id;
		}
		else {
			this.id = id.trim();
		}
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		
		if (style == null) {
			this.style = style;
		}
		else {
			this.style = style.trim();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name.trim();
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type.trim();
	}
	
	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		
		if (defaultValue == null) {
			this.defaultValue = defaultValue;
		}
		else {
			this.defaultValue = defaultValue.trim();
		}
	}
	
	public String getPleSelect() {
		return pleSelect;
	}

	public void setPleSelect(String pleSelect) {
		
		if (pleSelect == null) {
			this.pleSelect = pleSelect;
		}
		else {
			this.pleSelect = pleSelect.trim();
		}
	}
	
	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		
		if (onChange == null) {
			this.onChange = onChange;
		}
		else {
			this.onChange = onChange.trim();
		}
	}
	
	/**
	 * @throws IOException 
	 * 
	 */
	 public int doStartTag() throws JspException
	 {   
			if (this.type == null || "".equals(this.type)) {
				this.type = "isEmpty";
			}
			
			if (this.name == null || "".equals(this.name)) {
				this.name = "isEmpty";
			}
			
	       return TagSupport.SKIP_BODY;  
	  }   

	 
	 /**
		 * 
		 */
		public int doEndTag()  throws JspTagException 
		{
			 try {
				    StringBuffer pageBuffer = new StringBuffer();
				    
				    publicDictService = (IPublicDictService) SpringContextUtil.getBean("publicDictService");

					if (!"isEmpty".equals(this.type) && !"isEmpty".equals(this.name)) {
						List<PublicDict> lstDic = publicDictService.findByType(this.type);
						
						if (lstDic.size() > 0) {
							pageBuffer.append(" <select class='form-control input-sm' ");
							
							if (this.id != null && !"".equals(this.id)) {
								pageBuffer.append("id = '" + this.id + "' ");
							}
							
							pageBuffer.append("name = '" + this.name + "' ");
							
							if (this.style != null && !"".equals(this.style)) {
								pageBuffer.append("style = '" + this.style + "' ");
							}
							
							if (this.onChange != null && !"".equals(this.onChange)) {
								pageBuffer.append("onChange = '" + this.onChange + "' ");
							}
							
							pageBuffer.append(">");
							
							pageBuffer.append("<option value = ''>请选择...</option>");
							
							if (this.pleSelect != null && !"".equals(this.pleSelect)) {
								pageBuffer.append("<option value = ''>" + this.pleSelect + "</option>");
							}
							
							for (Iterator<PublicDict> iterator = lstDic.iterator(); iterator.hasNext();) {
								PublicDict dic = (PublicDict) iterator.next();
								pageBuffer.append("<option value = '" + dic.getDictName() + "' ");
								
								if (this.defaultValue != null && !"".equals(this.defaultValue) && this.defaultValue.equals(dic.getDictName())) {
									pageBuffer.append("selected='selected'");
								}
								
								pageBuffer.append(">" + dic.getDictName() + "</option>");
							}
							
							pageBuffer.append("</select>");
						}
						
					}
					
					pageContext.getOut().print(pageBuffer.toString());
			 }
			 catch(Exception ex)
			 {
				 try
				 {
					 ex.printStackTrace();
				     pageContext.getOut().print("<div> \n <p>加载数据发生错误<p> \n<p>" + ex.getMessage() +"<p> \n</div>");
				 }catch(Exception e)
				 {
				 }
			 }
			 
			 return TagSupport.SKIP_BODY ;
		}
}
